package home;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;

public class JDBCReg {
    public static void main(String[] args) {
        try (final Connection connection = DBUtil.getConnection();){
            final Statement statement = connection.createStatement();
            Scanner sc=new Scanner(System.in);
            System.out.println("请输入用户名:");
            String username=sc.nextLine();
            System.out.println("请输入密码:");
            String password =sc.nextLine();
            System.out.println("昵称:");
            String nickname =sc.nextLine();
            System.out.println("年龄:");
            int age=sc.nextInt();
            // 判断用户是否存在
            String sql2="select username from userinfo where username='"+username+"'";
            final ResultSet resultSet = statement.executeQuery(sql2);
            if (resultSet.next()){
                System.out.println("用户已存在,注册失败");
                return;
            }
            // 执行插入语句
            String sql="INSERT INTO userinfo(id,username,password,nickname,age) VALUES(NULL,'"+username+"','"+password+"','"+nickname+"',"+age+")";
            final int i = statement.executeUpdate(sql);
            if (i>0){
                System.out.println("注册成功");
            }else {
                System.out.println("注册失败");
            }

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }
}
